<?php

namespace App\Http\Controllers;

use App\Events\ChatMessageEvent;
use Illuminate\Http\Request;

class ChatController extends Controller
{
    /**
     * 显示聊天页面
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        return view('chat');
    }

    /**
     * 发送聊天消息
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function send(Request $request)
    {
        $request->validate([
            'username' => 'required|string|max:255',
            'message' => 'required|string',
        ]);

        event(new ChatMessageEvent($request->username, $request->message));

        return response()->json(['success' => true]);
    }
}
